package com.healthMedical.utils;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class sendMessage {
    /**
     * 发送邮件
     * @throws MessagingException
     */
    public static void sendMessage(String receive, String code) throws MessagingException {
        /**
         * @param Receive： -->> 收件人
         * @param Title -->> 标题
         * @param Content -->> 内容
         */
        String Receive = receive;
        String Title = "健康医疗验证码：";
        String Content = "您好，你目前正在绑定QQ。你的验证码为："+code;

        Properties properties = new Properties();
        //协议
        properties.put("mail.transport.protocol", "smtp");
        //服务器
        properties.put("mail.smtp.host", "smtp.qq.com");
        //端口
        properties.put("mail.smtp.port", 465);
        properties.put("mail.smtp.auth", "true");
        //SSL加密传输
        properties.put("mail.smtp.ssl.enable", "true");
        //true-->>在控制台显示信息，false-->>不显示
        properties.put("mail.debug", "false");
        //得到回话对象
        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        //发件人邮箱
        message.setFrom(new InternetAddress("2437026810@qq.com"));
        //设置收件人
        message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(Receive) });
        //邮箱标题
        message.setSubject(Title);
        //邮箱内容
        message.setText(Content);
        //创建对象
        Transport transport = session.getTransport();
        //连接服务
        /**
         * @param 第一个参数为自己QQ邮箱
         * @param 第二个参数为授权码（刚刚QQ空间获取的）
         */
        transport.connect("2437026810@qq.com", "pfabvvivxbrqeajh");
        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        //关闭服务
        transport.close();
    }

    public static void sendMessage(String receive, String code,String Date) throws MessagingException {
        /**
         * @param Receive： -->> 收件人
         * @param Title -->> 标题
         * @param Content -->> 内容
         */
        String Receive = receive;
        String Title = "健康医疗预约成功通知";
        String Content = "您好,你已在"+Date+"成功预约 体检项目，请按时到预约完成检测";

        Properties properties = new Properties();
        //协议
        properties.put("mail.transport.protocol", "smtp");
        //服务器
        properties.put("mail.smtp.host", "smtp.qq.com");
        //端口
        properties.put("mail.smtp.port", 465);
        properties.put("mail.smtp.auth", "true");
        //SSL加密传输
        properties.put("mail.smtp.ssl.enable", "true");
        //true-->>在控制台显示信息，false-->>不显示
        properties.put("mail.debug", "false");
        //得到回话对象
        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        //发件人邮箱
        message.setFrom(new InternetAddress("2437026810@qq.com"));
        //设置收件人
        message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(Receive) });
        //邮箱标题
        message.setSubject(Title);
        //邮箱内容
        message.setText(Content);
        //创建对象
        Transport transport = session.getTransport();
        //连接服务
        /**
         * @param 第一个参数为自己QQ邮箱
         * @param 第二个参数为授权码（刚刚QQ空间获取的）
         */
        transport.connect("2437026810@qq.com", "pfabvvivxbrqeajh");
        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        //关闭服务
        transport.close();
    }
}
